package com.sgb.kjwl.viewmodel.machine;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.im.immodel.IMUtils;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import org.json.JSONObject;

public class AskLeaseMyPriceDetailModel extends ViewModel {

    public MutableLiveData<String> showStateAcceptLiveData = new MutableLiveData<>();//接受状态
    public MutableLiveData<String> showStateCancelLiveData = new MutableLiveData<>();//已取消状态
    public MutableLiveData<String> showMyUrlLiveData = new MutableLiveData<>();//后台H5详情URL
    public MutableLiveData<String[]> showUrlLiveData = new MutableLiveData<>();//前台H5详情URL


    //接受报价
    public void acceptPrice(String rentalSeekId, String id,String categoryType) {
        APIManager.getInstance().getMachineAPI().acceptOffer(rentalSeekId, id, categoryType).enqueue(new ICallBack<BaseEntity>(true, true) {
            @Override
            public void onSuccess(BaseEntity data) {
                showStateAcceptLiveData.postValue("接受报价成功");
            }
        });
    }

    //取消报价
    public void cancelPrice(String id) {
        APIManager.getInstance().getMachineAPI().cancelQuote(id).enqueue(new ICallBack<BaseEntity>(true, true) {
            @Override
            public void onSuccess(BaseEntity data) {
                showStateCancelLiveData.postValue("取消报价成功");
            }
        });
    }

    public void getMyH5Url(String categoryType, String id) {
        String urlName = "";
        if ("0".equals(categoryType)) {//机械
            urlName = "mechanicsoffer";
        } else if ("1".equals(categoryType)) {//设备
            urlName = "equipmentoffer";
        } else if ("2".equals(categoryType)) {//车辆
            urlName = "vehicleoffer";
        } else if ("3".equals(categoryType)) {//房产
            urlName = "houseoffer";
        } else if ("4".equals(categoryType)) {//土地
            urlName = "landOfferDetails";
        } else if ("5".equals(categoryType)) {//工厂
            urlName = "factoryOfferDetails";
        } else if ("6".equals(categoryType)) {//农机
            urlName = "farmOfferDetails";
        }
        APIManager.getInstance().getLeaseAPI().getUrlSetting(urlName).enqueue(new ICallBack<BaseEntity<H5ReEntity>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + "?id=" + id;
                showMyUrlLiveData.postValue(url);
//                WebViewX5HostActivity.start(myPriceListFragment.getContext(), url);
            }
        });
    }


    public void getH5Url(String categoryType, String id) {
        String urlName = "";
        if ("0".equals(categoryType)) {//机械
            urlName = "mechanicsRentDetails";
        } else if ("1".equals(categoryType)) {//设备
            urlName = "equipmentRentDetails";
        } else if ("2".equals(categoryType)) {//车辆
            urlName = "vehicleRentDetails";
        } else if ("3".equals(categoryType)) {//房产
            urlName = "houseRentDetails";
        } else if ("4".equals(categoryType)) {//土地
            urlName = "landRentDetails";
        } else if ("5".equals(categoryType)) {//工厂
            urlName = "factoryRentDetails";
        } else if ("6".equals(categoryType)) {//农机
            urlName = "farmRentDetails";
        }
        if (TextUtils.isEmpty(urlName)) {
            return;
        }
        APIManager.getInstance().getPickAPI().getUrlSetting(urlName).enqueue(new ICallBack<BaseEntity<H5ReEntity>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + "?id=" + id;
                showUrlLiveData.postValue(new String[]{url, id, String.valueOf(Integer.parseInt(categoryType)+1)});//categoryType为详情分享使用，下标不一致需加1
//                WebViewX5HostActivity.start(askLeaseFragment.getActivity(), url, id, "asklease", categoryType);
            }
        });
    }

    //交流获取IM
    public void getImByCustomerId(String customerServiceContactsId) {
        APIManager.getInstance().getLeaseAPI().getImByCustomerId(customerServiceContactsId).enqueue(new ICallBack<BaseEntity>(true, true) {
            @Override
            public void onSuccess(BaseEntity data) {
                try {
                    JSONObject jsonObject = new JSONObject(data.getData().toString());
                    String imAccount = jsonObject.getString("imAccount");
                    IMUtils.getInstance().buildSingleTalking(imAccount);
                } catch (Exception ex) {
                }
            }
        });
    }

}
